package site.thatime.fnp_example;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;

import io.flutter.app.FlutterActivity;
import io.flutter.plugins.GeneratedPluginRegistrant;
import site.thatime.baozhuli.Result;
import site.thatime.fnp.FnpPlugin;
import site.thatime.tricycle.datetime.DateTime;
import site.thatime.tricycle.util.DateTimeUtil;
import top.someapp.applib.Base;
import top.someapp.applib.kits.Ctx;

public class MainActivity extends FlutterActivity implements Base.Command, Handler.Callback {
    private Base.FlutterNativePlugin plugin;
    private Handler handler;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        GeneratedPluginRegistrant.registerWith(this);
        plugin = FnpPlugin.getPlugin();
        plugin.setCommandProvider(this::getCommand);
        handler = new Handler(this);
        handler.sendEmptyMessageDelayed(0, 5 * 1000);
    }

    private Base.Command getCommand(Comparable id) {
        return this;
    }

    @Override
    public void execute(Base.MethodCallInfo methodCall, Result result) {
        Ctx.toastShort(this, "Native toast: request " + methodCall.methodName() + " received!");
        result.setMessage(methodCall.methodName()).end(true);
    }

    @Override
    public boolean handleMessage(Message msg) {
        if (msg.what == 0) {
            String now = DateTimeUtil.toIos8601(DateTime.now());
            plugin.getEventSender().onNativeEvent(Result.success("native_time", now));
            handler.sendEmptyMessageDelayed(0, 3 * 1000);
            return true;
        }
        return false;
    }
}
